﻿package 
{
    import ChatIconFile.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    import flash.utils.*;

    public class ChatIconFile extends Sprite
    {
        private var _changeNum:int = 0;
        private var _createType:int = 0;
        private var _mainNum:int = 0;
        private var _countNum:int = 0;
        private var _titleWidth:int = 0;
        public var Indent:int = 40;
        public var ShowWidth:int = 280;
        private var _showHeight:Number = 0;
        private var _link:int = 0;
        private var _oldLink:int = 0;
        private var _linkHeight:int = 15;
        private var _iconWidth:int = 26;
        private var _spacing:int = 0;
        private var _fontSize:int = 13;
        private var _addFile:Boolean = false;
        private var _index:int = 0;
        private var _chatFile:TextField;
        private var _mcBox:Sprite;
        private var _chatList:Array;
        private var _maxLink:int = 40;
        private var _removeLink:int = 20;
        public var isClick:Boolean = true;
        public var textColor:uint = 16777215;
        private var _onTextLink:Function;
        private var _onFileScor:Function;
        private var _textFormat:TextFormat;
        private var _dropFilter:DropShadowFilter;
        private var _chatObject:Object;
        private var _assetsData:Object;
        private var _mainHeight:Number = 0;

        public function ChatIconFile() : void
        {
            this._chatFile = new TextField();
            this._chatList = [];
            this._onTextLink = new Function();
            this._onFileScor = new Function();
            this.addTextForamt();
            this.addFilters();
            return;
        }// end function

        public function set onTextLink(param1:Function) : void
        {
            this._onTextLink = param1;
            return;
        }// end function

        public function set onFileScor(param1:Function) : void
        {
            this._onFileScor = param1;
            return;
        }// end function

        private function addTextForamt() : void
        {
            this._textFormat = new TextFormat();
            this._textFormat.size = this._fontSize;
            this._textFormat.font = "";
            this._textFormat.leading = 9;
            return;
        }// end function

        private function addFilters() : void
        {
            this._dropFilter = new DropShadowFilter();
            this._dropFilter.blurX = 3;
            this._dropFilter.blurY = 3;
            this._dropFilter.distance = 0;
            this._dropFilter.alpha = 0.6;
            this._dropFilter.strength = 10;
            this._dropFilter.color = 0;
            this._dropFilter.quality = 1;
            return;
        }// end function

        public function getChatData(param1:Object, param2:Boolean = true) : void
        {
            this.removeChatLimit();
            this._assetsData = {};
            this._assetsData.list = [];
            this._index = 0;
            this._oldLink = 0;
            this._countNum = 0;
            this._createType = 0;
            this._changeNum = 0;
            this._mainNum = 0;
            this._chatObject = this.copyData(param1);
            this.addMcBox();
            this.renderTitle(this._chatObject.title);
            this.renderNameIcon(this._chatObject.nameIcon);
            if (this._chatObject.isHtml)
            {
                this.renderName(this._chatObject.name + this._chatObject.msg);
                if (this._chatObject.isLostIcon)
                {
                    this.renderLostIcon(this._chatObject.isLostIcon);
                }
            }
            else
            {
                this.renderName(this._chatObject.name);
                this.renderChat(this._chatObject);
            }
            this._chatList.push(this._assetsData);
            if (param2)
            {
                this.scroChat();
            }
            return;
        }// end function

        public function getChatList(param1:Array) : void
        {
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            this.clear();
            var _loc_2:* = param1.length;
            var _loc_3:int = 0;
            if (_loc_2 >= this._removeLink)
            {
                _loc_3 = _loc_2 - this._removeLink;
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    param1.shift();
                    _loc_4 = _loc_4 + 1;
                }
            }
            _loc_2 = param1.length;
            _loc_4 = 0;
            while (_loc_4 < _loc_2)
            {
                
                _loc_5 = param1[_loc_4];
                this.getChatData(_loc_5, false);
                _loc_4 = _loc_4 + 1;
            }
            this.scroChat();
            return;
        }// end function

        private function copyData(param1:Object) : Object
        {
            var _loc_2:Object = {};
            if (!param1.title)
            {
            }
            _loc_2.title = "";
            if (!param1.name)
            {
            }
            _loc_2.name = "";
            _loc_2.nameIcon = [];
            if (!param1.isHtml)
            {
            }
            _loc_2.isHtml = false;
            _loc_2.msgColor = param1.msgColor;
            if (param1.isLostIcon != null)
            {
                _loc_2.isLostIcon = this.copyArray(param1.isLostIcon);
            }
            if (param1.nameIcon != null)
            {
                _loc_2.nameIcon = this.copyArray(param1.nameIcon);
            }
            if (!param1.msg)
            {
            }
            _loc_2.msg = "";
            _loc_2.icon = [];
            if (param1.icon != null)
            {
                _loc_2.icon = this.copyArray(param1.icon);
            }
            return _loc_2;
        }// end function

        public function copyArray(param1:Array) : Array
        {
            var _loc_5:Object = null;
            var _loc_2:Array = [];
            var _loc_3:* = param1.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = param1[_loc_4];
                _loc_2.push(_loc_5);
                _loc_4 = _loc_4 + 1;
            }
            return _loc_2;
        }// end function

        private function scroChat() : void
        {
            var _loc_3:Object = null;
            this._mainHeight = 0;
            var _loc_1:* = this._chatList.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._chatList[_loc_2];
                _loc_3.mcBox.y = this._mainHeight;
                this._mainHeight = this._mainHeight + (_loc_3.mcBox.height + this._spacing);
                _loc_2 = _loc_2 + 1;
            }
            this._onFileScor(this);
            return;
        }// end function

        private function addMcBox() : void
        {
            this._mcBox = new Sprite();
            this.addChild(this._mcBox);
            this._assetsData.mcBox = this._mcBox;
            return;
        }// end function

        private function getTextFile() : TextField
        {
            var _loc_1:* = new TextField();
            _loc_1.selectable = false;
            _loc_1.mouseWheelEnabled = false;
            _loc_1.autoSize = TextFieldAutoSize.LEFT;
            _loc_1.filters = [this._dropFilter];
            _loc_1.textColor = 16777215;
            return _loc_1;
        }// end function

        private function renderTitle(param1:String) : void
        {
            if (param1 != null)
            {
            }
            if (param1 == "")
            {
                return;
            }
            this._chatFile = this.getTextFile();
            this._chatFile.htmlText = param1;
            this._chatFile.y = 0;
            this._chatFile.setTextFormat(this._textFormat);
            this._mainNum = this._chatFile.width;
            this._mcBox.addChild(this._chatFile);
            if (this.isClick)
            {
                this._chatFile.addEventListener(TextEvent.LINK, this.textLink);
            }
            this._assetsData.list.push(this._chatFile);
            return;
        }// end function

        private function renderNameIcon(param1:Array) : void
        {
            var _loc_4:Object = null;
            var _loc_5:Class = null;
            var _loc_6:MovieClip = null;
            if (param1 != null)
            {
            }
            if (param1.length <= 0)
            {
                return;
            }
            var _loc_2:* = param1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = param1[_loc_3];
                this._mainNum = this._mainNum + this._changeNum;
                _loc_5 = getDefinitionByName(_loc_4.icon) as Class;
                _loc_6 = new _loc_5;
                _loc_6.x = this._mainNum;
                this._mcBox.addChild(_loc_6);
                this._changeNum = _loc_4.width == null ? (this._iconWidth) : (_loc_4.width);
                this._assetsData.list.push(_loc_6);
                _loc_3 = _loc_3 + 1;
            }
            return;
        }// end function

        private function renderLostIcon(param1:Array) : void
        {
            var _loc_4:Object = null;
            var _loc_5:Class = null;
            var _loc_6:MovieClip = null;
            var _loc_2:* = param1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = param1[_loc_3];
                this._mainNum = this._mainNum + this._changeNum;
                _loc_5 = getDefinitionByName(_loc_4.icon) as Class;
                _loc_6 = new _loc_5;
                _loc_6.x = _loc_4.x;
                _loc_6.y = _loc_4.y;
                this._mcBox.addChild(_loc_6);
                this._changeNum = _loc_4.width == null ? (this._iconWidth) : (_loc_4.width);
                this._assetsData.list.push(_loc_6);
                _loc_3 = _loc_3 + 1;
            }
            return;
        }// end function

        private function renderName(param1:String) : void
        {
            if (param1 != null)
            {
            }
            if (param1 == "")
            {
                return;
            }
            this._mainNum = this._mainNum + this._changeNum;
            this._chatFile = this.getTextFile();
            this._chatFile.htmlText = param1;
            this._chatFile.y = 0;
            this._chatFile.x = this._mainNum;
            this._chatFile.setTextFormat(this._textFormat);
            this._changeNum = this._chatFile.width;
            this._mcBox.addChild(this._chatFile);
            if (this.isClick)
            {
                this._chatFile.addEventListener(TextEvent.LINK, this.textLink);
            }
            this._assetsData.list.push(this._chatFile);
            return;
        }// end function

        private function renderChat(param1:Object) : void
        {
            var _loc_9:int = 0;
            if (param1.icon == null)
            {
            }
            if (param1.msg == null)
            {
                return;
            }
            var _loc_2:* = param1.msg;
            var _loc_3:* = _loc_2.length;
            if (!param1.icon)
            {
            }
            var _loc_4:Array = [];
            var _loc_5:* = _loc_4.length;
            if (this._index > (_loc_3 - 1))
            {
            }
            if (_loc_5 <= 0)
            {
                return;
            }
            var _loc_6:* = _loc_2.charAt(this._index);
            if (!_loc_4[0])
            {
            }
            var _loc_7:Object = null;
            var _loc_8:* = this._countNum + this._mainNum + this._changeNum;
            this._link = Math.floor(_loc_8 / this.ShowWidth);
            if (this._oldLink != this._link)
            {
                _loc_9 = this._link > 0 ? (this.Indent) : (0);
                this._oldLink = this._link;
                this._countNum = _loc_8;
                this._createType = 0;
                this._changeNum = 0;
                this._mainNum = _loc_9;
                this._addFile = false;
            }
            if (_loc_7 != null)
            {
            }
            if (_loc_7.index == this._index)
            {
                this.createIcon(_loc_7.icon);
            }
            else
            {
                this._addFile = true;
                this.createFile(_loc_6, param1, this._createType != 1);
            }
            return;
        }// end function

        private function createFile(param1:String, param2:Object, param3:Boolean) : void
        {
            if (param3)
            {
                this._mainNum = this._mainNum + this._changeNum;
                this._chatFile = this.getTextFile();
                this._chatFile.x = this._mainNum;
                this._chatFile.y = this._link * (this._fontSize + this._linkHeight);
                this._mcBox.addChild(this._chatFile);
                this._createType = 1;
                this._assetsData.list.push(this._chatFile);
            }
            this._chatFile.appendText(param1);
            this._chatFile.setTextFormat(this._textFormat);
            this._changeNum = this._chatFile.width;
            if (param2.msgColor)
            {
                this._chatFile.textColor = param2.msgColor;
            }
            var _loc_4:String = this;
            var _loc_5:* = this._index + 1;
            _loc_4._index = _loc_5;
            this.renderChat(this._chatObject);
            return;
        }// end function

        private function textLink(event:TextEvent) : void
        {
            this._onTextLink(event);
            return;
        }// end function

        private function createIcon(param1:String) : void
        {
            this._mainNum = this._mainNum + this._changeNum;
            var _loc_2:* = getDefinitionByName(param1) as Class;
            var _loc_3:* = new _loc_2;
            _loc_3.x = this._mainNum;
            var _loc_4:* = this._addFile == false ? (this._link * (this._fontSize + this._linkHeight)) : (this._chatFile.y);
            _loc_3.y = _loc_4 + (this._chatFile.textHeight - _loc_3.height) / 2;
            this._mcBox.addChild(_loc_3);
            this._chatObject.icon.shift();
            this._createType = 2;
            this._changeNum = this._iconWidth;
            this._assetsData.list.push(_loc_3);
            this.renderChat(this._chatObject);
            return;
        }// end function

        public function clear() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this._chatList.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._chatList[_loc_2];
                this.removeChatData(_loc_3);
                _loc_2 = _loc_2 + 1;
            }
            this._chatList = [];
            return;
        }// end function

        private function removeChatLimit() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this._chatList.length;
            if (_loc_1 < this._maxLink)
            {
                return;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._removeLink)
            {
                
                _loc_3 = this._chatList[_loc_2];
                this.removeChatData(_loc_3);
                this._chatList.splice(_loc_2, 1);
                _loc_2 = _loc_2 + 1;
            }
            this.scroChat();
            return;
        }// end function

        private function removeChatData(param1:Object) : void
        {
            var _loc_5:DisplayObject = null;
            var _loc_2:* = param1.list;
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = _loc_2[_loc_4] as DisplayObject;
                if (_loc_5 as TextField)
                {
                    _loc_5.removeEventListener(TextEvent.LINK, this.textLink);
                }
                _loc_5.parent.removeChild(_loc_5);
                _loc_5 = null;
                _loc_4 = _loc_4 + 1;
            }
            param1.mcBox.parent.removeChild(param1.mcBox);
            param1.mcBox = null;
            return;
        }// end function

        override public function get height() : Number
        {
            return this._mainHeight;
        }// end function

    }
}
